/*******************************************/
/*        (c) Pavel Bacovsky, Ph.D.        */
/*     			Bates College		       */
/*  	   Department of Politics		   */
/*           Lewiston, ME 04240            */
/*           pbacovsky@bates.edu           */  
/*             www.bacovsky.phd            */
/*******************************************/ 

clear
use "Bacovsky-LevelUpData_JEPS_Replication.dta", clear

net install grc1leg, from( http://www.stata.com/users/vwiggins/) 	// Installing the grc1leg package.
help grc1leg 												        // This package allows to create a combined figure with a single legend. Otherwise, it is the same as *graph combine*

****************************************************************
*  Cleanup of low-quality data (rqindex must be at least 0.6)  *
****************************************************************
drop if rqindex < 0.6

****************************************************************
* DV1: General interest in US foreign trade policy, post-manip *	// The following code recreates regression outputs presented in Table 2 and visualizations presented in Figure 4 in the main manuscript.
****************************************************************

reg dv11_1 c.pt2##treatment pt1										// IV: Importance of being a gamer.
margins, at (pt2=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g1, replace)

reg dv11_1 c.pt3##treatment pt1										// IV: How well "gamer" describes.
margins, at (pt3=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g2, replace)

reg dv11_1 c.pt4##treatment pt1										// IV: Gamers = we.
margins, at (pt4=(1(1)5) treatment = (0 1)) atmeans noatlegend		
marginsplot, recastci(rarea) name(g3, replace)

reg dv11_1 c.pt5##treatment pt1										// IV: Gamer self-image.
margins, at (pt5=(1(1)4) treatment = (0 1)) atmeans noatlegend		
marginsplot, recastci(rarea) name(g4, replace)

reg dv11_1 c.gid##treatment pt1										// Gamer Identity Index.
margins, at (gid=(1(.5)4) treatment = (0 1)) atmeans noatlegend		
marginsplot, recastci(rarea) name(g5, replace)

reg dv11_1 c.pt9##treatment pt1										// Time spent gaming, daily.
margins, at (pt9=(1(1)6) treatment = (0 1)) atmeans noatlegend		
marginsplot, recastci(rarea) name(g6, replace)

grc1leg g1 g2 g3 g4 g5 g6, ycommon altshrink commonscheme name(combined, replace)
graph display combined, xsize(6)


******************************************************* 
* DV2: Interest in the policy discussed in the prompt *			// The following code recreates regression outputs presented in Table 4 and visualizations presented in Figure 5 in the main manuscript.
*******************************************************

reg dv11_2 c.pt2##treatment										// IV: Importance of being a gamer.
margins, at (pt2=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g1, replace)

reg dv11_2 c.pt3##treatment										// IV: How well "gamer" describes.
margins, at (pt3=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g2, replace)

reg dv11_2 c.pt4##treatment										// IV: Gamers = we.
margins, at (pt4=(1(1)5) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g3, replace)

reg dv11_2 c.pt5##treatment										// IV: Gamer self-image.
margins, at (pt5=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g4, replace)

reg dv11_2 c.gid##treatment										// Gamer Identity Index.
margins, at (gid=(1(.5)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g5, replace)

reg dv11_2 c.pt9##treatment										// Time spent gaming, daily.
margins, at (pt9=(1(1)6) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g6, replace)

grc1leg g1 g2 g3 g4 g5 g6, ycommon altshrink commonscheme name(combined, replace)
graph display combined, xsize(6)

*****************************************************
*      Additional Tests and Robustness Checks       *
*                ONLINE APPENDIX 3                  *
*****************************************************

/* Normality of Dependent Variables */
 * DV: Post-treatment general interest in U.S. foreign trade policy, Figure A3.1
hist dv11_1, normal
qnorm dv11_1
swilk dv11_1
sfrancia dv11_1
sktest dv11_1

 * DV: Difference in pre- and post-treatment general interest in U.S. foreign trade policy, Figure A3.2
hist genint_diff, normal
qnorm genint_diff
swilk genint_diff
sfrancia genint_diff
sktest genint_diff

 * DV: Interest in U.S. foreign trade policy specifically discussed in the prompt, Figure A3.3
hist dv11_2, normal
qnorm dv11_2
swilk dv11_2
sfrancia dv11_2
sktest dv11_2

/* Homogeneity of Variance Tests */
 * DV: Post-treatment general interest
sdtest dv11_1, by(treatment)

egen cell1 = group(gid treatment)
egen cell2 = group(pt9 treatment)
egen cell3 = group(pt2 treatment)

robvar dv11_1, by(treatment)
robvar dv11_1, by(cell1)
robvar dv11_1, by(cell2)
robvar dv11_1, by(cell3)

 *DV: Pre- and post-treatment general interest difference
sdtest genint_diff, by(treatment)

/*
 * This code replicates the variables necessary for homegeneity of variance tests. They can be commented out unless the replication is only interested in homogeneity of variance in specific DV-IV relationships.
egen cell1 = group(gid treatment)
egen cell2 = group(pt9 treatment)
egen cell3 = group(pt2 treatment)
*/

robvar genint_diff, by(treatment)
robvar genint_diff, by(cell1)
robvar genint_diff, by(cell2)
robvar genint_diff, by(cell3)

 *DV: Post-treatment specific interest
sdtest dv11_2, by(treatment)

/*
 * This code replicates the variables necessary for homegeneity of variance tests. They can be commented out unless the replication is only interested in homogeneity of variance in specific DV-IV relationships.
egen cell1 = group(gid treatment)
egen cell2 = group(pt9 treatment)
egen cell3 = group(pt2 treatment)
*/

robvar dv11_2, by(treatment)
robvar dv11_2, by(cell1)
robvar dv11_2, by(cell2)
robvar dv11_2, by(cell3)

/* Additional analysis using ordered logit */
 * DV: Post-treatment general interest (Table A3.1)
ologit dv11_1 c.pt2##treatment pt1
ologit dv11_1 c.pt3##treatment pt1
ologit dv11_1 c.pt4##treatment pt1
ologit dv11_1 c.pt5##treatment pt1
ologit dv11_1 c.gid##treatment pt1
ologit dv11_1 c.pt9##treatment pt1
 
 *DV: Post-treatment specific interest 
ologit dv11_2 c.pt2##treatment
ologit dv11_2 c.pt3##treatment
ologit dv11_2 c.pt4##treatment
ologit dv11_2 c.pt5##treatment 
ologit dv11_2 c.gid##treatment
ologit dv11_2 c.pt9##treatment

/* Additional analysis using pre- and post-test interest difference as the DV, Table A3.3 and Figure 3.4 */
reg genint_diff c.pt2##treatment
margins, at (pt2=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g1, replace)

reg genint_diff c.pt3##treatment
margins, at (pt3=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g2, replace)

reg genint_diff c.pt4##treatment
margins, at (pt4=(1(1)5) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g3, replace)

reg genint_diff c.pt5##treatment
margins, at (pt5=(1(1)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g4, replace)

reg genint_diff c.gid##treatment

margins, at (gid=(1(.5)4) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g5, replace)

reg genint_diff c.pt9##treatment
margins, at (pt9=(1(1)6) treatment = (0 1)) atmeans noatlegend
marginsplot, recastci(rarea) name(g6, replace)

grc1leg g1 g2 g3 g4 g5 g6, ycommon altshrink commonscheme name(combined, replace)
graph display combined, xsize(6)

/* Additional analysis using Kruskal-Wallis sub-tests */
 * DV: Post-treatment general interest*
sum dv11_1 if treatment == 0
sum dv11_1 if treatment == 1
kwallis dv11_1, by(treatment)

sum dv11_1 if treatment == 0 & gid >= 2.5
sum dv11_1 if treatment == 1 & gid >= 2.5
kwallis dv11_1 if gid >= 2.5, by(treatment)

sum dv11_1 if treatment == 0 & gid < 2.5
sum dv11_1 if treatment == 1 & gid < 2.5
kwallis dv11_1 if gid < 2.5, by(treatment)
 
 * DV: Pre- and post-treatment general interest difference
sum genint_diff if treatment == 0
sum genint_diff if treatment == 1
kwallis genint_diff, by(treatment)

sum genint_diff if treatment == 0 & gid >= 2.5
sum genint_diff if treatment == 1 & gid >= 2.5
kwallis genint_diff if gid >= 2.5, by(treatment)

sum genint_diff if treatment == 0 & gid < 2.5
sum genint_diff if treatment == 1 & gid < 2.5
kwallis genint_diff if gid < 2.5, by(treatment)

 * DV: Post-treatment specific interest
sum dv11_2 if treatment == 0
sum dv11_2 if treatment == 1
kwallis dv11_2, by(treatment)

sum dv11_2 if treatment == 0 & gid >= 2.5
sum dv11_2 if treatment == 1 & gid >= 2.5
kwallis dv11_2 if gid >= 2.5, by(treatment)

sum dv11_2 if treatment == 0 & gid < 2.5
sum dv11_2 if treatment == 1 & gid < 2.5
kwallis dv11_2 if gid < 2.5, by(treatment)


/* Testing linearity of the interaction */
 * DV: Post-treatment specific interest (Figure A3.5)
twoway (sc dv11_1 gid) (lowess dv11_1 gid), by(treatment) // Relationship seems linear
interflex dv11_1 treatment gid
twoway (sc dv11_1 pt9) (lowess dv11_1 pt9), by(treatment) // Relationship seems linear
interflex dv11_1 treatment pt9

 * DV: Post-treatment specific interest (Figure A.6)
twoway (sc dv11_2 gid) (lowess dv11_2 gid), by(treatment) // Relationship seems linear
interflex dv11_2 treatment gid
twoway (sc dv11_2 pt9) (lowess dv11_2 pt9), by(treatment) // Relationship seems linear
interflex dv11_2 treatment pt9